﻿using System.IO;
using System.Windows.Forms;
using DataAccessLayer;
using Editor.Properties;

namespace Editor
{
    public sealed class BusinessLogicLayer : IBusinessLogicLayer
    {
        private static IBusinessLogicLayer staticInstance;

        private const string TopLevelFolder = @"..\..\";
        private const string MenuBkgsFolder = @"Content\Menu Background";
        private const string SoundsFolder = @"Content\Sounds";
        private const string FontsFolder = @"Content\Fonts";
        private const string ProtagonistSpritesFolder = @"Content\Sprites\Protagonists";
        private const string EnemySpritesFolder = @"Content\Sprites\Enemies";
        private const string BulletSpritesFolder = @"Content\Sprites\Bullets";
        private const string AnomalySpritesFolder = @"Content\Sprites\Anomalies";
        private const string LevelBkgFolder = @"Content\Level Backgrounds";

        private BusinessLogicLayer() { }

        public static IBusinessLogicLayer UniqueInstance
        {
            get { return staticInstance ?? (staticInstance = new BusinessLogicLayer()); }
        }

        public EditorStartData LoadApplicationData(string filename)
        {
            EditorStartData startData = new EditorStartData();

            try
            {
                startData.GameData = ObjectSerializer.Deserialize<GameData>(filename);
            }
            catch 
            {
                MessageBox.Show(Resources.LoadDataFailErr, Resources.GameEditorTitle);
            }
            finally
            {
                if(null == startData.GameData)
                {
                    startData.GameData = new GameData();
                }

                startData.FontFiles = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, FontsFolder));
                startData.MenuBackgrounds = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, MenuBkgsFolder));
                startData.SoundFiles = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, SoundsFolder));
                startData.ProtagonistSprites = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, ProtagonistSpritesFolder));
                startData.EnemySprites = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, EnemySpritesFolder));
                startData.BulletSprites = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, BulletSpritesFolder));
                startData.AnomalySprites = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, AnomalySpritesFolder));
                startData.LevelBackgrounds = Directory.GetFiles(string.Format("{0}{1}", TopLevelFolder, LevelBkgFolder));
            }

            return startData;
        }

        public string[] GetFiles(string dir)
        {
            return Directory.GetFiles(dir);
        }

        public void SaveGameFile(GameData data, string filename)
        {
            ObjectSerializer.Serialize(data, filename);
        }
    }
}